package xyz.coolcsm.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.coolcsm.security.entity.Comment;
import xyz.coolcsm.security.entity.SysUser;
import xyz.coolcsm.security.entity.SysUserInfo;
import xyz.coolcsm.security.mapper.CommentMapper;
import xyz.coolcsm.security.service.CommentService;
import xyz.coolcsm.security.service.QuestionService;
import xyz.coolcsm.security.service.SysUserInfoService;
import xyz.coolcsm.security.service.SysUserService;
import xyz.coolcsm.security.utils.UUIDUtils;

import java.util.List;
/**
 * @author 什锦
 * @since 2021-06-1
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper,Comment> implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    SysUserInfoService sysUserInfoService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    QuestionService questionService;


    @Override
    public void insertComment(String username, Comment comment) {
        SysUser sysUser = sysUserService.selectByName(username);
        SysUserInfo sysUserInfo = sysUserInfoService.getById(sysUser.getId());
        comment.setUserName(sysUserInfo.getNickname());
        comment.setUserAvatar(sysUserInfo.getAvatar());
        comment.setUserId(sysUserInfo.getUid());
        comment.setCommentId(UUIDUtils.getUuid());
        comment.setTopicTitle("1");
        commentMapper.insert(comment);
    }

    @Override
    public List<Comment> getCommentByQid(Integer qid) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("qid",qid)
        .orderByDesc("create_time");
        List<Comment> comments = commentMapper.selectList(wrapper);
        return comments;
    }

    @Override
    public Page<Comment> pageByQid(Integer qid, Integer limit, Integer page,Integer tcid) {
        Page<Comment> pageParam = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("qid",qid)
                .orderByDesc("create_time")
                .eq("topic_category",tcid);
        commentMapper.selectPage(pageParam,wrapper);
        return pageParam;
    }


}
